﻿@{
    ViewBag.Title = "Recreate Search Index";
}

<h1>@ViewBag.Title</h1>

<h2><span id="instructions">Please do not browse away from this page!!!<br />You will be notified when you can.</span></h2>
<div id="progresscontainer" style="width: 400px">
<div id="pb1"></div>
<div>Plugin being processed: <span id="PluginName">None</span>&nbsp;&nbsp;<span id="Counter">0</span> of <span id="PluginCount">unknown</span></div>
<p><br /><br /></p>
<div id="pb2"></div>
<div>Overall progress: <span id="DocumentCount">0</span> of <span id="TotalDocumentCount">unknown</span></div>
</div>
<script type="text/javascript" language="javascript">

    var pdata;
    var totaldocsindexed = 0.0;
    var plugindocsindexed = 0;
    var iPartition = 0;
    var iPlugin = 0;
    var totaldocs = 0;
    var timer;
    var TIMEOUT = 0;

    function FindPluginWithData() {
        iPartition = 0;
        plugindocsindexed = 0;
        if (iPlugin >= pdata.PluginCount) return;
        while (pdata.Plugins[iPlugin].DocumentCount == 0) {
            iPlugin++;
            if (iPlugin >= pdata.PluginCount) return;
        }
    }

    function ProcessPartition() {
        clearTimeout(timer);
        if (iPlugin < pdata.PluginCount) {
            var url = "";
            $("#PluginName").html(pdata.Plugins[iPlugin].PluginName);
            $("#Counter").html(iPlugin + 1);
            url = "/" + pdata.Plugins[iPlugin].Area + "/" + pdata.Plugins[iPlugin].Controller + "/Reindex";
            var retval = 0;
            $.ajax({
                "url": url, type: "POST", dataType: "json",
                "async": false,
                "data": { "partition": iPartition, "IsReturnDocCountRequest": false },
                "success": function (ivalue) {
                    retval = ivalue;
                    if (ivalue >= 0) {
                        plugindocsindexed += ivalue;
                        totaldocsindexed += ivalue;
                    }
                }
            });
            // Update the progressbar for this plugin
            value = 0;
            if (plugindocsindexed >= pdata.Plugins[iPlugin].DocumentCount)
                value = 100;
            else
                value = (plugindocsindexed * 100.0) / pdata.Plugins[iPlugin].DocumentCount;
            $("#pb1").progressbar("option", "value", value);
            // Update the Overall Progressbar
            if (totaldocs != 0) {
                value = (totaldocsindexed * 100) / totaldocs;
                $("#pb2").progressbar("option", "value", value);
                $("#DocumentCount").html(totaldocsindexed);
            }
            if (retval < 0) {
                iPlugin++;
                FindPluginWithData();
                if (iPlugin < pdata.PluginCount)
                    timer = setTimeout(ProcessPartition, TIMEOUT);
                else
                    $("#instructions").html("Completed!");
            }
            else {
                iPartition++;
                timer = setTimeout(ProcessPartition, TIMEOUT); 
            }
        }
        else
            $("#instructions").html("Completed!");
    }

    function ProcessPlugins(data) {
        pdata = data;
        $("#PluginCount").html(pdata.PluginCount);

        // Gather the counts of documents to be indexed by Plugin and the total document count
        totaldocs = 0;
        var i = 0;
        for (i = 0; i < pdata.PluginCount; i++) {
            var plugin = pdata.Plugins[i];
            var url = "/" + plugin.Area + "/" + plugin.Controller + "/Reindex";
            $.ajax({
                "url": url, type: "POST", dataType: "json",
                "async": false,
                "data": { "partition": 0, "IsReturnDocCountRequest": true },
                "success": function (ivalue) {
                    pdata.Plugins[i].DocumentCount = ivalue;
                }
            });
            totaldocs += pdata.Plugins[i].DocumentCount
        }
        $("#TotalDocumentCount").html(totaldocs);

        // Iterate through the plugins and index the documents by iterating through partitions.
        // When all the partitions in a plugin are exhausted, the conttroller returns a value less than 0.
        // Use setTimeout to process each partition so that the UI gets a chance to refresh

        iPlugin = 0;
        FindPluginWithData();
        timer = setTimeout(ProcessPartition, TIMEOUT); 
    }

    $(document).ready(function () {
        $("#pb1").progressbar({ value: 0 });
        $("#pb2").progressbar({ value: 0 });
        $.ajax({
            url: "/Setup/Plugins", type: "POST", dataType: "json",
            success: ProcessPlugins
        });
    });
</script>
